import { InjectionKey } from 'vue';
import createPersistedstate from 'vuex-persistedstate';
import { createStore, Store, useStore as baseUseStore, createLogger, Commit } from 'vuex';
import storage from '@/utils/storage';
import User from '@/store/modules/index';
import Content from '@/store/modules/content';
import GroupCode from '@/store/modules/groupCode';
import NewCode from '@/store/modules/newCode';
import Menu from '@/store/modules/menu';
import PageCache from '@/store/modules/pageCache';

// export interface State {}
// 定义 injection key
export const zwScrm: InjectionKey<Store<any>> = Symbol();

// 非生产环境打开调试
const debug: boolean = import.meta.env.MODE != 'prod';

const createLoggerProfile = () => {
    return debug ? createLogger() : () => {};
};
// 自定义持久化插件
const createPersistedstateProfile = () => {
    return createPersistedstate({
        // storage: window.sessionStorage,
        key: 'zw-scrm-state',
        // 请按需存储
        // paths: ['user'],
        reducer(state: any) {
            return {
                user: state.user,
                menu: state.menu,
                pageCache: state.pageCache
            };
        }
    });
};

export const store = createStore<any>({
    strict: false,
    state: {
        currentMenuId: localStorage.menuid || -1,
        btnCodes: [],
        // 表格
        table: {
            tableData: [], // 选中表格数据
            tableAll: false // 是否是全选
        },
        //删除弹框
        remove: {
            content: '确认删除所选内容吗？',
            delVisible: false,
            IsBatch: false //是否是批量删除
        },
        pageTotal: 0, // 总数量
        slideBar: JSON.parse(localStorage.current_menu || '{}'), // 选中的
        tabId: sessionStorage.acticvIndex || '-1',
        tabMenu: JSON.parse(sessionStorage.tabMenu || '[]'),
        group_id: JSON.parse(sessionStorage.group_id || '0'), // 分组ID记忆
        poster_group_type: JSON.parse(sessionStorage.poster_group_type || 9) // 海报中心分组类型ID记忆
    },
    mutations: {
        setCurrentMenuId: (state: any, menuid: number): void => {
            state.currentMenuId = menuid;
        },
        collectCode: (state: any, data: string): void => {
            state.btnCodes.push(data);
            // localStorage.setItem('btnCode',JSON.stringify(state.btnCodes))
            storage.set('power-code', state.btnCodes);
        },
        TABLE: (state: any, data: any): void => {
            state.table = {
                ...state.table,
                ...data
            };
        },
        DEL_DIALOG: (state: any, data: any): void => {
            state.remove = {
                ...state.remove,
                ...data
            };
        },
        PAGE_TOTAL: (state: any, menuid: number): void => {
            state.pageTotal = menuid;
        },
        GET_SLIDEBAR: (state: any, data: any): void => {
            state.slideBar = data;
        },
        GET_TABID: (state: any, data: any): void => {
            state.tabId = data;
        },
        GET_TAB_MENU: (state: any, data: any): void => {
            state.tabMenu = data;
        },
        SET_GROUP_ID: (state: any, data: any) => {
            state.group_id = data;
        },
        SET_POSTER_GROUP_TYPE: (state: any, data: any) => {
            state.poster_group_type = data;
        }
    },
    actions: {
        setCurrentMenuId: (context: { commit: Commit; state: any }, menuid: number): void => {
            localStorage.setItem('menuid', String(menuid));
            context.commit('setCurrentMenuId', menuid);
        },
        collectCode: ({ commit }, data: string): void => {
            commit('collectCode', data);
        },
        table: ({ commit }, data: any): void => {
            commit('TABLE', data);
        },
        delDialog: ({ commit }, data: any): void => {
            commit('DEL_DIALOG', data);
        },
        pageTotal: ({ commit }, data: number): void => {
            commit('PAGE_TOTAL', data);
        },
        getCurrentMenu: ({ commit }, data: any): void => {
            localStorage.setItem('current_menu', JSON.stringify(data));
            commit('GET_SLIDEBAR', data);
        },
        getTabId: ({ commit }, data: any): void => {
            sessionStorage.setItem('acticvIndex', data);
            commit('GET_TABID', data);
        },
        getTabMenu: ({ commit }, data: any): void => {
            sessionStorage.setItem('tabMenu', JSON.stringify(data));
            commit('GET_TAB_MENU', data);
        },
        setGroupId: ({ commit }, data: any): void => {
            sessionStorage.setItem('group_id', JSON.stringify(data));
            commit('SET_GROUP_ID', data);
        },
        setPosterGroupType: ({ commit }, data: any): void => {
            sessionStorage.setItem('poster_group_type', JSON.stringify(data));
            commit('SET_POSTER_GROUP_TYPE', data);
        }
    },
    getters: {
        // 获取当前选中菜单
        currentMenuId: (state: any): any => {
            if (state.currentMenuId <= 0) {
                return 1;
            }
            return state.currentMenuId;
        },
        // 获取权限控制的按钮集合
        getActiveCodes: (state: any): string[] => {
            return state.btnCodes;
        },
        table: (state: any) => state.table,
        delDialog: (state: any) => state.remove,
        pageTotal: (state: any) => state.pageTotal,
        getCurrentMenu: (state: any) => state.slideBar,
        getTabId: (state: any) => state.tabId,
        getTabMenu: (state: any) => state.tabMenu,
        group_id: (state: any) => state.group_id,
        poster_group_type: (state: any) => state.poster_group_type
    },
    modules: {
        user: User,
        content: Content,
        groupCode: GroupCode,
        newCode: NewCode,
        menu: Menu,
        pageCache: PageCache
    },
    plugins: debug
        ? [createLoggerProfile(), createPersistedstateProfile()]
        : [createLoggerProfile(), createPersistedstateProfile()]
});

// 自定义 `useStore` 组合式函数
export function useStore() {
    return baseUseStore(zwScrm);
}
